//
//  HMHomeTableViewCell.swift
//  Weibo
//
//  Created by 黄亦寒 on 2016/9/24.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit
//首页自定义cell
/*
 分为三部分去创建：1.原创微博视图；2.转发微博视图；3.底部tooBar视图
 */

//cell子控件之间的间距
let HMHomeCellMargin:CGFloat = 10

class HMHomeTableViewCell: UITableViewCell {

    //记录toolBar的顶部约束
    var toolBarTopConstraint:Constraint?
    
    //cell对应的viewModel
    var statusViewModel:HMStatusViewModel? {
        didSet {
            //给cell的子控件绑定数据
            originalView.statusViewModel = statusViewModel
            toolBar.statusViewModel = statusViewModel!
            //去除toolBar的顶部约束,根据有无转发微博更新约束
            toolBarTopConstraint?.uninstall()
            //判断转发微博是否为空
            if statusViewModel?.status.retweeted_status != nil {
                //如果不为空，转发微博不隐藏，更新toolBar的顶部约束等于转发微博的底部,然后绑定数据
                toolBar.snp_makeConstraints(closure: { (make) in
                    toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                retweetView.isHidden = false
                retweetView.statusViewModel = statusViewModel
                
            }else{
                //如果为空，就把转发微博隐藏起来，toolBar的顶部约束等于原创微博的底部
                retweetView.isHidden = true
                toolBar.snp_makeConstraints(closure: { (make) in
                    toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
        }
    }
    
    

    //懒加载控件
    //原创微博
    private lazy var originalView:HMStatusOriginalView = {
        let view = HMStatusOriginalView()
        return view
    }()
    
    //底部的toolBar
    private lazy var toolBar:HMStatusToolBar = HMStatusToolBar()
    
    
    //转发微博
    private lazy var retweetView:HMStatusRetweetView = {
        let view = HMStatusRetweetView()
        return view
    }()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置控件
    private func setUpUI() {
        contentView.backgroundColor = UIColor(white:0.95,alpha:1)
        addSubview(originalView)
        addSubview(toolBar)
        addSubview(retweetView)
        
        originalView.snp_makeConstraints { (make) in
            make.left.right.equalTo(self)
            make.top.equalTo(self).offset(8)
            //make.height.equalTo(100)
        }
        
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.left.right.equalTo(originalView)
            
        }
        
        toolBar.snp_makeConstraints { (make) in
           toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.left.right.equalTo(retweetView)
            make.height.equalTo(35)
            
            //把toolBar底部约束设置为cell的ContentView的底部
            make.bottom.equalTo(self.snp_bottom)
        }
        
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
